home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet internetowy / Przegladarki internetowe / Mozilla Seamonkey 1.0.5 pl / seamonkey-1.0.5.pl-PL.win32.installer.exe / BROWSER.XPI / bin / chrome / toolkit.jar / content / global / printProgress.js < prev    next >
Encoding:
Text File  |  2004-07-16  |  6.7 KB  |  220 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Mozilla Communicator client code, released
  16.  * March 31, 1998.
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Netscape Communications Corporation.
  20.  * Portions created by the Initial Developer are Copyright (C) 1998
  21.  * the Initial Developer. All Rights Reserved.
  22.  *
  23.  * Contributor(s):
  24.  *   William A. ("PowerGUI") Law <law@netscape.com>
  25.  *   Scott MacGregor <mscott@netscape.com>
  26.  *   jean-Francois Ducarroz <ducarroz@netscape.com>
  27.  *   Rod Spears <rods@netscape.com>
  28.  *   Karsten "Mnyromyr" D├╝sterloh <mnyromyr@tprac.de>
  29.  *
  30.  * Alternatively, the contents of this file may be used under the terms of
  31.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  32.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  33.  * in which case the provisions of the GPL or the LGPL are applicable instead
  34.  * of those above. If you wish to allow use of your version of this file only
  35.  * under the terms of either the GPL or the LGPL, and not to allow others to
  36.  * use your version of this file under the terms of the MPL, indicate your
  37.  * decision by deleting the provisions above and replace them with the notice
  38.  * and other provisions required by the GPL or the LGPL. If you do not delete
  39.  * the provisions above, a recipient may use your version of this file under
  40.  * the terms of any one of the MPL, the GPL or the LGPL.
  41.  *
  42.  * ***** END LICENSE BLOCK ***** */
  43.  
  44. // deck index constants
  45. const TITLE_COMPLETE_DECK = 1;
  46. const PROGRESS_METER_DECK = 1;
  47.  
  48.  
  49. // global variables
  50. var dialog;                 // associative array with various properties from the dialog document
  51. var percentFormat;          // format string for percent value
  52. var printProgress  = null;  // nsIPrintProgress
  53. var progressParams = null;  // nsIPrintProgressParams
  54. var switchUI       = true;  // switch UI on first progress change
  55.  
  56.  
  57. // all progress notifications are done through the nsIWebProgressListener implementation...
  58. var progressListener =
  59. {
  60.   onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus)
  61.   {
  62.     if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_START)
  63.     {
  64.       // put progress meter in undetermined mode
  65.       setProgressPercentage(-1);
  66.     }
  67.     if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
  68.     {
  69.       // we are done printing; indicate completion in title area
  70.       dialog.titleDeck.selectedIndex = TITLE_COMPLETE_DECK;
  71.       setProgressPercentage(100);
  72.       window.close();
  73.     }
  74.   },
  75.  
  76.   onProgressChange: function(aWebProgress,      aRequest,
  77.                              aCurSelfProgress,  aMaxSelfProgress,
  78.                              aCurTotalProgress, aMaxTotalProgress)
  79.   {
  80.     if (switchUI)
  81.     {
  82.       // first progress change: show progress meter
  83.       dialog.progressDeck.selectedIndex = PROGRESS_METER_DECK;
  84.       dialog.cancel.removeAttribute("disabled");
  85.       switchUI = false;
  86.     }
  87.     setProgressTitle();
  88.  
  89.     // calculate percentage and update progress meter
  90.     if (aMaxTotalProgress > 0)
  91.     {
  92.       var percentage = Math.round(aCurTotalProgress * 100 / aMaxTotalProgress);
  93.       setProgressPercentage(percentage);
  94.     }
  95.     else
  96.     {
  97.       // progress meter should be barber-pole in this case
  98.       setProgressPercentage(-1);
  99.     }
  100.   },
  101.  
  102.   onLocationChange: function(aWebProgress, aRequest, aLocation)
  103.   {
  104.     // we can ignore this notification
  105.   },
  106.  
  107.   onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage)
  108.   {
  109.     if (aMessage)
  110.       dialog.title.value = aMessage;
  111.   },
  112.  
  113.   onSecurityChange: function(aWebProgress, aRequest, aStatus)
  114.   {
  115.     // we can ignore this notification
  116.   },
  117.  
  118.   QueryInterface : function(iid)
  119.   {
  120.     if (iid.equals(Components.interfaces.nsIWebProgressListener) || iid.equals(Components.interfaces.nsISupportsWeakReference))
  121.       return this;
  122.     throw Components.results.NS_NOINTERFACE;
  123.   }
  124. };
  125.  
  126.  
  127. function setProgressTitle()
  128. {
  129.   if (progressParams)
  130.   {
  131.     dialog.title.crop  = progressParams.docTitle ? "end" : "center";
  132.     dialog.title.value = progressParams.docTitle || progressParams.docURL;
  133.   }
  134. }
  135.  
  136.  
  137. function setProgressPercentage(aPercentage)
  138. {
  139.   // set percentage as text if non-negative
  140.   if (aPercentage < 0)
  141.   {
  142.     dialog.progress.mode = "undetermined";
  143.     dialog.progressText.value = "";
  144.   }
  145.   else
  146.   {
  147.     dialog.progress.removeAttribute("mode");
  148.     dialog.progress.value = aPercentage;
  149.     dialog.progressText.value = percentFormat.replace("#1", aPercentage);
  150.   }
  151. }
  152.  
  153.  
  154. function onLoad()
  155. {
  156.   // set global variables
  157.   printProgress = window.arguments[0];
  158.   if (!printProgress)
  159.   {
  160.     dump("Invalid argument to printProgress.xul\n");
  161.     window.close();
  162.     return;
  163.   }
  164.  
  165.   dialog = {
  166.     titleDeck   : document.getElementById("dialog.titleDeck"),
  167.     title       : document.getElementById("dialog.title"),
  168.     progressDeck: document.getElementById("dialog.progressDeck"),
  169.     progress    : document.getElementById("dialog.progress"),
  170.     progressText: document.getElementById("dialog.progressText"),
  171.     cancel      : document.documentElement.getButton("cancel")
  172.   };
  173.   percentFormat = dialog.progressText.getAttribute("basevalue");
  174.   // disable the cancel button until first progress is made
  175.   dialog.cancel.setAttribute("disabled", "true");
  176.  
  177.   // XXX we could probably get rid of this test, it should never fail
  178.   if (window.arguments.length > 1 && window.arguments[1])
  179.   {
  180.     progressParams = window.arguments[1].QueryInterface(Components.interfaces.nsIPrintProgressParams);
  181.     setProgressTitle();
  182.   }
  183.  
  184.   // set our web progress listener on the helper app launcher
  185.   printProgress.registerListener(progressListener);
  186.   printProgress.doneIniting();
  187. }
  188.  
  189.  
  190. function onUnload()
  191. {
  192.   if (printProgress)
  193.   {
  194.     try
  195.     {
  196.       printProgress.unregisterListener(progressListener);
  197.       printProgress = null;
  198.     }
  199.     catch(ex){}
  200.   }
  201. }
  202.  
  203.  
  204. // If the user presses cancel, tell the app launcher and close the dialog...
  205. function onCancel()
  206. {
  207.   // cancel app launcher
  208.   try
  209.   {
  210.     printProgress.processCanceledByUser = true;
  211.   }
  212.   catch(ex)
  213.   {
  214.     return true;
  215.   }
  216.   // don't close dialog by returning false, the backend will close the dialog
  217.   // when everything will be aborted.
  218.   return false;
  219. }
  220.